//
//  PhotoViewController.m
//  WheatPlan
//
//  Created by 王晨辉 on 15/7/8.
//  Copyright (c) 2015年 IOSTeam. All rights reserved.
//

#import "PhotoViewController.h"
#import "PhotoTableViewCell.h"
@interface PhotoViewController ()

@end

@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    //设置导航栏头文字
    self.navigationItem.title = @"家园统合";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:UIColorWithRGBA(239, 88, 88, 1)];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    

    
    UIImageView *tableBack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BG@2x.png.png"]];

    _PhotoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _PhotoTableView.delegate = self;
    _PhotoTableView.dataSource = self;
    //    _evaluationTableView.separatorInset = UIEdgeInsetsZero;
    //    _evaluationTableView.separatorColor = [UIColor blueColor];
    _PhotoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _PhotoTableView.backgroundView = tableBack;
    [self.view addSubview:_PhotoTableView];
    
    
    aimages = [NSMutableArray array];
    atimeStamp = [NSMutableArray array];
    atitle = [NSMutableArray array];
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *ID = [user objectForKey:@"ID"];

    NSDictionary *param = @{@"userId": ID};
    
    [UserDAL getBabyHistoryParams: param withBlock:^(NSArray *data, NSError *error) {
        if (data) {
//           ![data isMemberOfClass:[NSNull class]] 
            
             if (data.count!=0) {
            for (NSDictionary *myDic in data) {
                
 
                NSArray * images = myDic[@"images"];
                
                
                NSString * timeStamp = [myDic objectForKey:@"timeStamp"];
                NSString * title = [myDic objectForKey:@"title"];
                
                
                [aimages addObject:images];
                [atimeStamp addObject:timeStamp];
                [atitle addObject:title];
                          

                
            }
            
            [_PhotoTableView reloadData];
            
             }
            
            
             else{
                 
                 
                 alert(@"会员未绑定宝宝");
                 
             }
            
            
        }
        else
        {
            
            
        }
        
        
        
    }];

    
    
    
    
    
    
    //自定义左右按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(turntolast)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;

    
    // Do any additional setup after loading the view.
}
-(void)turntolast{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    
    return atitle.count;
    
    
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if ([atitle[indexPath.row] isEqualToString:@""]) {
        
        
        return  90+40;

        
        
    }else if (aimages[indexPath.row] == nil){
        
        
        
        return  60;
    }else{
        
        
        
        
        return  130+40;
        

    }
    
    
    
}



#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    static NSString *NOTIFY = @"cell";
//    PhotoTableViewCell *cell = (PhotoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    
    static NSString *NOTIFY = @"cellSetOutVC";
    PhotoTableViewCell *cell = (PhotoTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if (cell==nil) {
        
        cell=[[PhotoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
        
        
//        cell.otherLab.text = @"额呵呵";
//        cell.otherLab.frame = CGRectMake(50, 10, 100, 30);
//        
//        cell.childeImageView.frame = CGRectMake(40, 50, 60, 50);
//        cell.childeImageView.backgroundColor = [UIColor redColor];
        
    }
    
    
    
    cell.backgroundColor = [UIColor clearColor];
    cell.userInteractionEnabled = NO;
    
    
    UIImageView * imageBG = [[UIImageView alloc]initWithFrame:CGRectMake(47/2-4, 5, 10, 10)];
    imageBG.image = [UIImage imageNamed:@"round@2x.png.png"];
    [cell addSubview:imageBG];
    
    
    if ([atitle[indexPath.row] isEqualToString:@""]) {
        
        
        
        
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(47/2+10, 10, SCREEN_WIDTH-67, 120)];
        image.image = [UIImage imageNamed:@"tokeAbout.png"];
        [cell addSubview:image];
        
        int lie = 3;
        
        arrra = [NSMutableArray  array];
        //        arrra = aimages[indexPath.row];
        
        for (NSDictionary * dic in aimages[indexPath.row]) {
            
            NSArray * arr = [dic objectForKey:@"imageUrl"];
            [arrra addObject:arr];
            ////NSLog(@"hahahahaahahahah%@",arrra);
            
            
        }
        
        
        for (int i = 0; i < arrra.count; i ++)
        {
            int X = i % lie;
//            int Y = i / lie;
            
            
            UIImageView * childeImageViews = [[UIImageView alloc]initWithFrame:CGRectMake(10+(((SCREEN_WIDTH-67)-20)/3+5)*X, 7, ((SCREEN_WIDTH-67)-20)/3, 120-14)];
            [childeImageViews sd_setImageWithURL:arrra[i] placeholderImage:nil];
            [image addSubview:childeImageViews];
            
            
        }
        
        
        
        
        
        
        
        
        
        UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(47/2, 0, 3, 90+40)];
        lineView2.backgroundColor = [UIColor whiteColor];
        [cell addSubview:lineView2];

        
        
        
        
        
        
    }else if (aimages[indexPath.row] == nil){
        
        
        
//        _PhotoTableView.rowHeight = 60;
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(47/2+10, 10, SCREEN_WIDTH-67, 40)];
        image.image = [UIImage imageNamed:@"tokeAbout.png"];
        [cell addSubview:image];
        
        
        cell.otherLab.text = [NSString stringWithFormat:@"%@",atitle[indexPath.row]];
        cell.otherLab.frame = CGRectMake(20, 0, 100, 30);
        [image addSubview:cell.otherLab];
        
        
        UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(47/2, 0, 3, 60)];
        lineView2.backgroundColor = [UIColor whiteColor];
        
        
        
        [cell addSubview:lineView2];

        
        
        
    }else{
        
        
        
        
//        _PhotoTableView.rowHeight = 130+40;
        
        
        
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(47/2+10, 10, SCREEN_WIDTH-67, 150)];
        image.image = [UIImage imageNamed:@"tokeAbout.png"];
        [cell addSubview:image];
        
        
        cell.otherLab.text = [NSString stringWithFormat:@"%@",atitle[indexPath.row]];
        cell.otherLab.frame = CGRectMake(20, 0, 220, 30);
        cell.otherLab.numberOfLines = 0;
        [image addSubview:cell.otherLab];
        
        int lie = 3;
        
        arrra = [NSMutableArray  array];
//        arrra = aimages[indexPath.row];

        for (NSDictionary * dic in aimages[indexPath.row]) {
            
            NSArray * arr = [dic objectForKey:@"imageUrl"];
            [arrra addObject:arr];
            ////NSLog(@"hahahahaahahahah%@",arrra);

            
        }
        
        
        for (int i = 0; i < arrra.count; i ++)
        {
            int X = i % lie;
//            int Y = i / lie;
            
            
            UIImageView * childeImageViews = [[UIImageView alloc]initWithFrame:CGRectMake(10+(((SCREEN_WIDTH-67)-20)/3+5)*X, 30+7, ((SCREEN_WIDTH-67)-20)/3, 120-14)];
            [childeImageViews sd_setImageWithURL:arrra[i] placeholderImage:nil];
            [image addSubview:childeImageViews];
            
            
        }
        
        
        UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(47/2, 0, 3, 130+40)];
        lineView2.backgroundColor = [UIColor whiteColor];
        [cell addSubview:lineView2];

        
        
        
        
        
        
    }
    
    
    
    
    return cell;
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    NSString *msg = [[NSString alloc] initWithFormat:@"您点击的是第%d个按钮!",buttonIndex];
    ////NSLog(@"msg:%@",msg);
    if (buttonIndex == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        ////NSLog(@"1111111");
        
    }else if(buttonIndex == 1){
        
        
        ////NSLog(@"2222222");
        
        
    }
    
    
}


#pragma mark--- tableViewDelegate 有几个区
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
        
    }
#pragma mark--- tableViewDelegat 点击方法
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        

        
        
        


}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
